package great.easychat.help.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import great.easychat.help.R;


/**
 * 提示框
 */
public class CommonDialog {
    private AlertDialog dialog = null;

    public interface Callback {
        void sure();

        void cancel();
    }


    private Callback mcallback;

    public void setMcallback(Callback mcallback) {
        this.mcallback = mcallback;
    }

    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;
    TextView tvCancel,tvContent;
    public void show(Activity context, final Callback callback) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            mcallback = callback;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            dialog.setCancelable(false);//屏蔽返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.common_dialog);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            tvCancel = window.findViewById(R.id.tvCancel);
            if (isHideCancel) {
                tvCancel.setVisibility(View.GONE);
            }

            tvContent = window.findViewById(R.id.tvContent);
            if (!TextUtils.isEmpty(msg)) {
                tvContent.setText(msg);
            }
            TextView tvSure = window.findViewById(R.id.tvSure);
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (callback != null) {
                        callback.sure();
                    }
                    cancelDialog();
                }
            });

            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (callback != null) {
                        callback.cancel();
                    }
                    cancelDialog();
                }
            });
        }
    }

    boolean isHideCancel;
    String msg;
    public void hideCancel() {
        isHideCancel = true;
    }

    public void setContent(String content) {
        msg = content;
    }

    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
